<template>
  <CustomDialog
    v-model:visible="isShowDialog"
    :is-close="false"
    :curentClass="'create-dishes-dialog'"
  >
    <div class="dialog-wrap" v-loading="dialogLoading">
      <div class="dialog-content">
        <div class="dialog-header-box"></div>
        <div class="title">生成菜谱</div>
        <div class="img-box">
          <img @click="onCancel" src="@/assets/images/components/dialog-close.png" alt="" />
        </div>
      </div>
      <div class="dialog-gap"></div>

      <div class="dishes-list-box">
        <div class="loop-box">
          <div class="dishes-list-item" v-for="(item, index) in dishesList" :key="index">
            <div class="img-box">
              <img
                v-if="item.foodInfo?.images?.length"
                :src="item.foodInfo.images[0].file.preview_url"
                class="img"
                alt=""
              />
              <img v-else src="@/assets/images/home/not-goodimage.png" class="img" alt="" />
            </div>
            <div class="dishes-content-box">
              <span class="dishes-name">{{ item.foodInfo?.name }}</span>
              <span v-if="currentDeskDetail?.user?.grade" class="dishes-price">
                ￥{{ item.member_price }}/{{ item.unit }}
              </span>
              <span v-else class="dishes-price">￥{{ item.price }}/{{ item.unit }}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="dishes-category-box">
        <span class="symbol">*</span>
        <span class="title">菜谱分类</span>
        <el-select
          v-model="params.category_id"
          clearable
          placeholder="请选择菜谱分类"
          style="width: 300px"
        >
          <el-option
            v-for="item in menuCateList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>

      <div class="dishes-name-box">
        <span class="symbol">*</span>
        <span class="title">菜谱名称</span>
        <el-input
          class="input"
          style="width: 300px"
          placeholder="请输入菜谱名称"
          v-model="params.name"
        />
      </div>

      <div class="dialog-gap"></div>
      <div class="receive-bottom-btn">
        <el-button class="cancel-btn" @click="onCancel">取消</el-button>
        <el-button class="confirm-btn" type="primary" @click="onConfirm" :loading="confirmLoading"
          >确定</el-button
        >
      </div>
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { orderMenuFoodList, orderMenuFoodAdd, menuCategoryList } from '@/api/food'
import { throttle } from '@/utils/toolsValidate'
import { useUserStore } from '@/stores/user'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import { ElMessage } from 'element-plus'

interface Props {
  createVisible: boolean
  orderId?: string | number
}
const props = defineProps<Props>()
const emit = defineEmits(['update:createVisible'])

const isShowDialog = computed({
  get: () => props.createVisible,
  set: v => emit('update:createVisible', v)
})
const dishesList = ref<any[]>([])
const params = ref<{ name: string | null; category_id: number | null; order_id?: string | number }>(
  { name: null, category_id: null }
)
const menuCateList = ref<any[]>([])
const confirmLoading = ref(false)
const dialogLoading = ref(false)

const user = useUserStore()
const currentDeskDetail = ref<any>(user?.desktopWorkStatus || null)

function resetForm() {
  params.value = { name: null, category_id: null }
  dishesList.value = []
}

watch(isShowDialog, async val => {
  if (val) {
    dialogLoading.value = true
    try {
      await Promise.all([getMenuCategoryList(), getOrderMenuFoodList()])
    } finally {
      dialogLoading.value = false
    }
  } else {
    resetForm()
  }
})

function getMenuCategoryList() {
  return menuCategoryList({}).then(res => {
    if (res.status === 200) menuCateList.value = res.data
  })
}
function getOrderMenuFoodList() {
  return orderMenuFoodList({ order_id: props.orderId }).then(res => {
    if (res.status === 200) dishesList.value = res.data
  })
}

function onCancel() {
  emit('update:createVisible', false)
  resetForm()
}

const onConfirm = throttle(async () => {
  // const testForm: Record<string, any> = {
  //   请选择菜品分类: params.value.category_id,
  //   请输入菜谱名称: params.value.name
  // }
  if (!params.value.category_id || !params.value.name) return
  params.value.order_id = props.orderId
  try {
    confirmLoading.value = true
    const res = await orderMenuFoodAdd(params.value)
    if (res.status === 200) {
      ElMessage.success('生成菜谱成功')
      emit('update:createVisible', false)
      resetForm()
    }
  } finally {
    confirmLoading.value = false
  }
}, 1000)
</script>

<style scoped lang="scss">
::v-deep {
  .el-dialog__body {
    padding: 0;
    border-radius: 20px;
  }
  .el-dialog__header {
    padding: 0;
  }
  .el-dialog {
    border-radius: 20px;
  }
}
.dialog-content {
  display: flex;
  width: 100%;
  padding-right: 30px;
  box-sizing: border-box;
  padding-top: 20px;
}
.dialog-content .dialog-header-box {
  width: 33%;
}
.dialog-content .title {
  width: 33%;
  font-size: 20px;
  font-weight: 700;
  color: #333;
  text-align: center;
}
.dialog-content .img-box {
  width: 33%;
  display: flex;
  justify-content: flex-end;
}
.dialog-content .img-box img {
  width: 28px;
  height: 28px;
  cursor: pointer;
}
.dialog-gap {
  width: 100%;
  height: 1px;
  background: #f2f2f2;
  margin-top: 20px;
}
.dishes-list-box {
  width: 100%;
  max-width: 1040px;
  height: 380px;
  background: #f8f8f8;
  display: flex;
  flex-direction: column;
  padding: 16px;
  justify-content: flex-start;
  //   margin-top: 16px;
  overflow-y: auto;
  margin-left: 0;
  box-sizing: border-box;
}
.dishes-list-box .loop-box {
  display: grid;
  flex-wrap: wrap;
  grid-template-columns: repeat(5, 1fr);
  //   overflow: hidden;
  gap: 22px;
}
.dishes-list-box .dishes-list-item {
  width: 150px;
  height: 183px;
  //   margin-right: 20px;
  cursor: pointer;
}
.dishes-list-box .dishes-list-item .img-box {
  width: 150px;
  height: 112px;
  border-radius: 12px 12px 0 0;
}
.dishes-list-box .dishes-list-item .img-box .img {
  width: 100%;
  height: 100%;
  border-radius: 12px 12px 0 0;
}
.dishes-list-box .dishes-list-item .dishes-content-box {
  padding: 10px;
  display: flex;
  flex-direction: column;
  background: #fff;
}
.dishes-list-box .dishes-list-item .dishes-content-box .dishes-name {
  font-size: 18px;
  font-weight: 700;
  color: #131432;
  margin-bottom: 6px;
  max-width: 300px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.dishes-list-box .dishes-list-item .dishes-content-box .dishes-price {
  font-size: 14px;
  color: #333;
}
.dishes-list-box .loop-box::-webkit-scrollbar {
  width: 6px !important;
  height: 0 !important;
}
.dishes-list-box .loop-box::-webkit-scrollbar-thumb {
  height: 200px !important;
  background: #d2d2d2 !important;
  border-radius: 3px !important;
}
.dishes-category-box {
  margin-top: 20px;
  display: flex;
  align-items: center;
  margin-left: 0;
  padding-left: 20px;
}
.dishes-category-box .symbol {
  color: #ff372b;
}
.dishes-category-box .title {
  margin-right: 10px;
}
.dishes-name-box {
  margin-top: 16px;
  display: flex;
  align-items: center;
  margin-left: 0;
  padding-left: 20px;
}
.dishes-name-box .symbol {
  color: #ff372b;
}
.dishes-name-box .title {
  margin-right: 10px;
}
.receive-bottom-btn {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-right: 0;
  padding-right: 20px;
}
.receive-bottom-btn .cancel-btn {
  margin: 20px 0;
  min-width: 140px;
  height: 52px;
  border-radius: 12px;
  font-size: 18px;
}
.receive-bottom-btn .confirm-btn {
  margin: 20px 0 20px 40px;
  min-width: 140px;
  height: 52px;
  border-radius: 12px;
  font-size: 18px;
}

.create-dishes-dialog {
  padding: 0px !important;
}
</style>
<style lang="scss">
.create-dishes-dialog {
  padding: 0px !important;
}
</style>
